package com.hdoit.kylin.web.group.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.kylin.logical.group.model.LessonTrain;
import com.hdoit.kylin.logical.group.service.ILessonTrainService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.LessonTrainSearch;

/**
 * @author luozhicheng
 * @email
 * @createtime 2017年07月26日 19:05:35
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/lessonTrain/")
public class LessonTrainController extends BaseController {
	@Override
	public String getRoutePath() {
		return "areas/admin/lessonTrain/";
	}

	private ITeacherService teacherService;
	public ITeacherService getTeacherService() {
		return teacherService;
	}
	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	private ILessonTrainService lessonTrainService;

	public ILessonTrainService getLessonTrainService() {
		return lessonTrainService;
	}

	@Resource
	public void setLessonTrainService(ILessonTrainService lessonTrainService) {
		this.lessonTrainService = lessonTrainService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "lessonTrainDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";

	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel,
			LessonTrainSearch search, boolean ajaxPost) {
		pagerModel.setTotalAmount(lessonTrainService.queryCount(search));
		List<LessonTrain> lessonTrains = lessonTrainService.queryList(search,
				pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("lessonTrains", lessonTrains);
		model.addAttribute("lessonTrainSearch", search);
		if (ajaxPost) {
			return view(outputViewName);
		}
		return view(indexViewName);
	}

	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String lessonTrainDetailed(Model model, String key) {
		LessonTrain lessonTrain = new LessonTrain();
		if (!StringUtils.isEmpty(key)) {
			lessonTrain = lessonTrainService.queryModel(key);
		}
		model.addAttribute("lessonTrain", lessonTrain);
		return view(detailedViewName);
	}

	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model, LessonTrain lessonTrain) {
		if (StringUtils.isEmpty(lessonTrain.getId())) {
			lessonTrain.setId(CommonUtils.getKeyId());
			lessonTrain.setGroupId(teacherService.queryModel(getLoginModel().getLoginId()).getGroupId());
			lessonTrain.setTeacherName(teacherService.queryModel(lessonTrain.getDutyId()).getEmployeeName());
			lessonTrain.setModelStatus(ModelStatus.add);
		} else {
			lessonTrain.setTeacherName(teacherService.queryModel(lessonTrain.getDutyId()).getEmployeeName());
			lessonTrain.setModelStatus(ModelStatus.update);
		}
		ActionResult actionResult;
		if (lessonTrainService.applyChange(lessonTrain)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}
		model.addAttribute("lessonTrain", lessonTrain);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		return view(detailedViewName);
	}

	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (lessonTrainService.deleteModel(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}